<?php
function replaceTex($pathOfTexFile, $loginUsername)
{
	// mysql connection info
	$conHost = "localhost"; 	// connect ip
	$conUsername = "test";
	$conPassword = "test";
	$conDatabase = "formatPager";
	$conView = "VIEW_backEndsData";
	$conQuery = "SELECT * FROM  $conView  WHERE username = '" . $loginUsername . "';";
	
	// connect mysql
    $con = mysql_connect($conHost, $conUsername, $conPassword);
    if (!$con){
        die('Could not connect: ' . mysql_error());
    }
	echo "Connect success.<br/>\n";
	
	// query database
    mysql_select_db($conDatabase, $con);
    $result = mysql_query("SET NAMES 'UTF8'");
    $result = mysql_query($conQuery, $con);
	echo "Query success.<br/>\n";
	
    $row = mysql_fetch_array($result);
    $i = 0;
    do{
        $rowname = mysql_field_name($result, $i);
        $infoarr[$rowname] = $row[$rowname];
        $i++;
        //echo $rowname."==".$infoarr[$rowname]."<br>";
    }while($rowname);
    mysql_close($con);

    $fstr = file_get_contents($pathOfTexFile);

    foreach ($infoarr as $key => $value){
        $fstr = str_replace("!".$key."!", "$value", $fstr);
    }
	
	// save tex file to 
    file_put_contents("newfile.tex", $fstr);
    $fpnew = fopen("newfile.tex", "r") or die("Cannot find new file!");
    
    return $fpnew;
}
?>
